//
//  SDetailsViewController.m
//  小胖兔旅游指南
//
//  Created by 小胖兔 on 15/4/14.
//  Copyright (c) 2015年 小胖兔. All rights reserved.
//

#import "SDetailsViewController.h"
#import "UIScrollView+PullLoad.h"
#import "SDetailsTableViewCell.h"
#import "SDDetailsViewController.h"
#import "AFNetworkTool.h"

@interface SDetailsViewController ()<UITableViewDelegate, UITableViewDataSource, PullDelegate>

@end

@implementation SDetailsViewController

- (void)dealloc
{
    [_OneID release];
    [_tableArray release];
    [_tableView release];
    [_OneName release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.alpha = 1;
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:58.0 / 255.0 green:165.0 / 255.0 blue:222.0 / 255.0 alpha:1]];
    [self.navigationController.navigationBar setTranslucent:NO];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNature];
    [self createTableView];
    [self requestData];


}

#pragma mark -
#pragma 改变本页自身属性
- (void)setNature
{
    [self.view setBackgroundColor:[UIColor whiteColor]];
    UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, 20, [[UIScreen mainScreen] bounds].size.width - 160, 30)];
    [aLabel setText:self.OneName];
    [aLabel setTextAlignment:NSTextAlignmentCenter];
    [aLabel setTextColor:[UIColor whiteColor]];
    [self.navigationItem setTitleView:aLabel];
}

#pragma mark -
#pragma 创建tableview
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    [self.tableView setBackgroundColor:[UIColor colorWithRed:231.0 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1.0]];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setPullDelegate:self];
    
    [self.tableView setCanPullDown:YES];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView registerClass:[SDetailsTableViewCell class] forCellReuseIdentifier:@"SDetailsCell"];
    
    
}

#pragma mark -
#pragma 下拉刷新
- (void)scrollView:(UIScrollView *)scrollView loadWithState:(LoadState)state
{
    if (state == PullDownLoadState) {
        [self performSelector:@selector(downLoad) withObject:nil];
    }
}



- (void)downLoad
{
    [self.tableView reloadData];
    [self.tableView stopLoadWithState:PullDownLoadState];
}




#pragma mark -
#pragma 请求数据
- (void)requestData
{

    SDetailsViewController *my = self;
    NSString *urlStr = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v1/trail/type/%@", self.OneID];
    NSDictionary *dic = @{@"app_version": @"1.9.0", @"device_type": @"1", @"page": @"1", @"page_size": @"20"};
    [AFNetworkTool JSONDataWithUrl:urlStr parameter:dic success:^(id json) {
        my.tableArray = [json objectForKey:@"trails"];
        [my.tableView reloadData];
    } fail:^{
        nil;
    }];
}


#pragma mark -
#pragma tableview协议方法实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ([[UIScreen mainScreen] bounds].size.width - 10) / 239.89 * 134.76 + 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SDetailsCell"];
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"xiaopangtu" ofType:@"jpg"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    [cell.aImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"cover"]] placeholderImage:image];
    [cell.aLabel setText:[dic objectForKey:@"name"]];
    NSString *place = [NSString stringWithFormat:@"🏁 %@", [dic objectForKey:@"destination"]];
    [cell.bLabel setText:place];
    // 让几个星星变白
    [cell.starRateView setScorePercent:(([[dic objectForKey:@"trail_score"] floatValue] / 0.5 + 1) / 20)];


    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SDDetailsViewController *sdDetail = [[SDDetailsViewController alloc] init];
    [sdDetail setTwoID:[[self.tableArray objectAtIndex:indexPath.row] objectForKey:@"id"]];
    [sdDetail setTwoName:[[self.tableArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
    [sdDetail setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:sdDetail animated:YES];
    [sdDetail release];

}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
